package net.bitacademy.java23.filters;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class CharEncodingFilter implements Filter {
	String charset = "UTF-8";
	 
	@Override
	public void init(FilterConfig config) throws ServletException {
		if (config.getInitParameter("charset") != null) {
			charset = config.getInitParameter("charset");
		}
	}
	
	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain nextChain) throws IOException, ServletException {
		request.setCharacterEncoding(charset);
		
		nextChain.doFilter(request, response);
		
	}

	@Override
	public void destroy() {}

}
